﻿using RedisHelp;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace StackExchange.Redis_Demo
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            RedisHelper redis = new RedisHelper(1);

            new Random().NextBytes(kb4);
            new Random().NextBytes(kb8);

            Console.WriteLine("开始测试");
            #region hash8K测试每秒3000,单线程1800
            //System.Threading.Tasks.Task.Factory.StartNew(() =>
            //{
            //    while (true)
            //    {
            //        增加100个key(redis);
            //    }
            //});

            //System.Threading.Tasks.Task.Factory.StartNew(() =>
            //{

            //    while (true)
            //    {
            //        增加100个key(redis);
            //    }
            //});
            #endregion


            #region 批量写入
            //System.Threading.Tasks.Task.Factory.StartNew(() =>
            //{

            //    while (true)
            //    {
            //        生成批量写入数据();
            //    }
            //}); System.Threading.Tasks.Task.Factory.StartNew(() =>
            //{

            //    while (true)
            //    {
            //        生成批量写入数据();
            //    }
            //});




            //System.Threading.Tasks.Task.Factory.StartNew(() =>
            //{

            //    while (true)
            //    {
            //        测试批量写入(redis);
            //    }
            //});


            System.Threading.Tasks.Task.Factory.StartNew(() =>
            {

                while (true)
                {
                    测试批量写入1(redis);
                }
            });
            #endregion




            //System.Threading.Tasks.Task.Factory.StartNew(() =>
            //{
            //    var r2 = regionFactory.Create<string, byte[]>("r2");
            //    while (true)
            //    {
            //        增加100个key(r2);
            //    }
            //});
            long per = 0;
            DateTime dt = DateTime.Now;

            System.Threading.Tasks.Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    long sp = kn - per;
                    per = kn;
                    double ss = (sp) / (DateTime.Now - dt).TotalSeconds;
                    dt = DateTime.Now;
                    System.Threading.Thread.Sleep(5000);
                    Console.WriteLine("速度：{0:F2}", ss);

                }
            });



            Console.WriteLine("开始测试Redis");

            Console.Read();


        }

        static long kn = 0;

        static byte[] kb8 = new byte[1024 * 8];
        static byte[] kb4 = new byte[1024 * 4];
        static void 增加100个key(RedisHelper redis)
        {
            for (int i = 0; i < 100; i++)
            {
                var key = i.ToString();
                redis.HashSetBytes(key, "Rdb", kb8);
                System.Threading.Interlocked.Increment(ref kn);
            }
        }

        static void 删除100个key(RedisHelper redis)
        {
            for (int i = 0; i < 100; i++)
            {
                var key = i.ToString();
                //var ddd = reg[key];
                //reg.Remove(key);
                byte[] s = redis.HashGetBytes(key, "Rdb");

            }
        }


        static System.Collections.Concurrent.BlockingCollection<List<KeyValuePair<RedisKey, RedisValue>>> bolck = new System.Collections.Concurrent.BlockingCollection<List<KeyValuePair<RedisKey, RedisValue>>>(20);

        static Random dom = new Random();

        static void 生成批量写入数据()
        {

            List<KeyValuePair<RedisKey, RedisValue>> list = new List<KeyValuePair<RedisKey, RedisValue>>();
            for (int i = 0; i < 1000; i++)
            {
               // kb8 = new byte[8 * 1024];
                //dom.NextBytes(kb8);
                var key = i.ToString();
                list.Add(new KeyValuePair<RedisKey, RedisValue>(key, kb8));
            }
            bolck.Add(list);
        }

        static void 测试批量写入(RedisHelper redis)
        {
 
            var list = bolck.Take();
             redis.StringSet(list);
             System.Threading.Interlocked.Add(ref kn, list.Count);
         }
        static void 测试批量写入1(RedisHelper redis)
        {
            List<KeyValuePair<RedisKey, RedisValue>> list = new List<KeyValuePair<RedisKey, RedisValue>>();
            for (int i = 0; i < 1000; i++)
            {
                kb8 = new byte[8 * 1024];
                dom.NextBytes(kb8);
                var key = i.ToString();
                list.Add(new KeyValuePair<RedisKey, RedisValue>(key, kb8));
            }
            redis.StringSet(list);
            System.Threading.Interlocked.Add(ref kn, list.Count);
         }


        #region 试用代码
        public void ccc()
        {

            RedisHelper redis = new RedisHelper(1);

            #region String

            string str = "123";
            Demo demo = new Demo()
            {
                Id = 1,
                Name = "123"
            };
            var resukt = redis.StringSet("redis_string_test", str);
            var str1 = redis.StringGet("redis_string_test");
            redis.StringSet("redis_string_model", demo);
            var model = redis.StringGet<Demo>("redis_string_model");

            for (int i = 0; i < 10; i++)
            {
                redis.StringIncrement("StringIncrement", 2);
            }
            for (int i = 0; i < 10; i++)
            {
                redis.StringDecrement("StringIncrement");
            }
            redis.StringSet("redis_string_model1", demo, TimeSpan.FromSeconds(10));

            #endregion String

            #region List

            for (int i = 0; i < 10; i++)
            {
                redis.ListRightPush("list", i);
            }

            for (int i = 10; i < 20; i++)
            {
                redis.ListLeftPush("list", i);
            }
            var length = redis.ListLength("list");

            var leftpop = redis.ListLeftPop<string>("list");
            var rightPop = redis.ListRightPop<string>("list");

            var list = redis.ListRange<int>("list");

            #endregion List

            #region Hash

            redis.HashSet("user", "u1", "123");
            redis.HashSet("user", "u2", "1234");
            redis.HashSet("user", "u3", "1235");
            var news = redis.HashGet<string>("user", "u2");

            #endregion Hash

            #region 发布订阅

            redis.Subscribe("Channel1");
            for (int i = 0; i < 10; i++)
            {
                redis.Publish("Channel1", "msg" + i);
                if (i == 2)
                {
                    redis.Unsubscribe("Channel1");
                }
            }

            #endregion 发布订阅

            #region 事务

            var tran = redis.CreateTransaction();

            tran.StringSetAsync("tran_string", "test1");
            tran.StringSetAsync("tran_string1", "test2");
            bool committed = tran.Execute();

            #endregion 事务

            #region Lock

            var db = redis.GetDatabase();
            RedisValue token = Environment.MachineName;
            if (db.LockTake("lock_test", token, TimeSpan.FromSeconds(10)))
            {
                try
                {
                    //TODO:开始做你需要的事情
                    Thread.Sleep(5000);
                }
                finally
                {
                    db.LockRelease("lock_test", token);
                }
            }

            #endregion Lock


        }
        #endregion

    }

    public class Demo
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}